package com.stu.day04;

import java.util.Arrays;

/**
 * 数组拷贝
 * @author zy
 * @date 2025/1/25 16:57
 */
public class ArraysCopyDemo {
    public static void main(String[] args) {
        int[] arr1 = new int[]{1,2,3,4,5};
        int[] arr2 = {6,7,8,9};
        int[][] arr3 = new int[][]{{1,2,3},{4,5},{6}};

        int[]arr11 = Arrays.copyOf(arr1, arr1.length);  //Arrays.copyOf返回的是一个拷贝数组
        System.out.println(Arrays.toString(arr11));  //Arrays.toString字符串返回一维数组

        int[][]arr13 = Arrays.copyOf(arr3, arr3.length);  //也可拷贝二维数组
        System.out.println(Arrays.deepToString(arr13));  //Arrays.seepToString字符串返回二维数组
        for (int[] arr : arr13) {
            for(int num : arr) {
                float num2 = num;
                System.out.printf("%-6.1f",num2);  //6表示占位符内容宽度,-表示左对齐,.1保留一位小数
            }
        }
        System.out.println();

        int[] arr12 = new int[5];
        System.arraycopy(arr2, 0, arr12, 0, arr2.length);  //更为灵活的数组拷贝
        System.out.println(Arrays.toString(arr12));

    }
}
